﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;

namespace LxCFD
{
    public partial class ZoneCFDFields
    {
        protected FvZone Zone;
        protected PhysicalModel PhysicalModel;
        protected MaterialData Material;
        protected Value1Base<double> Density;
        public void CheckCreate(FvZone zone)
        {
            this.Zone = zone;
            this.PhysicalModel = zone.PhysicalModel;
            if (PhysicalModel == null)
            {
                LxConsole.Error(string.Format($"区域{zone.name}未选择物理模型"));
                ComputeState.ReportInterrupt_FindError();
                return;
            }
            this.Material = PhysicalModel.MaterialData;
            if (Material == null)
            {
                LxConsole.Error(string.Format($"区域{zone.name}未选择材质参数"));
                ComputeState.ReportInterrupt_FindError();
                return;
            }

            CheckCreate_Vel(true);
            CheckCreate_T();
            this.Density = Material.ConstDensity.GetCopy(zone);
            SetParameter_Vel();
            SetParameter_T();

            PhysicalModel = null;
            Material = null;
        }

        public double[] GetArray(ScalarTypes scalarTypes)
        {
            switch (scalarTypes)
            {
                case ScalarTypes.Temperature: return T?.x;
                case ScalarTypes.MassImbalance: return Velocity?.MassImbalance;
                case ScalarTypes.Vel_U: return Velocity?.U.x;
                case ScalarTypes.Vel_V: return Velocity?.V.x;
                case ScalarTypes.Vel_W: return Velocity?.W.x;
                case ScalarTypes.Turb_k: return kw?.k.x;
                case ScalarTypes.Turb_w: return kw?.w.x;
                case ScalarTypes.Pressure: return P.x;
                case ScalarTypes.PressureCorrection: return PCor.x;
                default: return null;
            }
        }

    }
}
//public virtual void MakeCellStatistic(UserMonitor userMonitor, int taskIndex)
//{

//}
//public virtual void MakeFaceStatistic(UserMonitor userMonitor, IndexNumber indexNumber, int taskIndex)
//{
//    indexNumber.FaceIDs.GetSeg(out int fi1, out int fendp1, taskIndex);
//    indexNumber.CellIDs.GetSeg(out int ci1, out int cendp1, taskIndex);
//    Mesh.Face[] faces = zone.faces;
//    if (userMonitor.faceStatisticTyye == FaceStatisticTypes.面积)
//    {
//        double area = 0;
//        for (; fi1 < fendp1; fi1++)
//        {
//            area += faces[fi1].Area;
//        }
//        userMonitor.AddToSum(area, 0);
//        userMonitor.SetHasDataTrue(taskIndex);
//    }
//    else
//    {
//        double[] array = getArray(userMonitor.valueType);
//        if (array == null) return;
//        double total = 0, weight = 0;
//        switch (userMonitor.faceStatisticTyye)
//        {
//            case FaceStatisticTypes.最大值:
//                userMonitor.FindMax(ci1, cendp1, array, taskIndex);
//                break;
//            case FaceStatisticTypes.最小值:
//                userMonitor.FindMin(ci1, cendp1, array, taskIndex);
//                break;
//            case FaceStatisticTypes.求和:
//            case FaceStatisticTypes.平均:
//                weight = cendp1 - ci1;
//                for (; ci1 < cendp1; ci1++)
//                {
//                    total += array[ci1];
//                }
//                userMonitor.AddToSum(total, weight);
//                userMonitor.SetHasDataTrue(taskIndex);
//                break;
//            case FaceStatisticTypes.几何积分:
//                for (; ci1 < cendp1; ci1++, fi1++)
//                {
//                    total += array[ci1] * faces[fi1].Area;
//                }
//                userMonitor.AddToSum(total, weight);
//                userMonitor.SetHasDataTrue(taskIndex);
//                break;
//            case FaceStatisticTypes.几何平均:
//                for (; ci1 < cendp1; ci1++, fi1++)
//                {
//                    double area = faces[fi1].Area;
//                    total += array[ci1] * area;
//                    weight += area;
//                }
//                userMonitor.AddToSum(total, weight);
//                userMonitor.SetHasDataTrue(taskIndex);
//                break;
//            default:
//                break;
//        }
//    }

//}
//public virtual void PickDataFromOld(Mesh.Zone zone, FieldsBase_InZone oldfields)
//{

//}
//public static void CheckFieldsAndFinishSetting(Mesh.Zone zone)
//{
//    FieldsBase_InZone newfield = null;

//    switch (zone.continuum.physicalType)
//    {
//        case PhysicalTypes.固体:
//            if (zone.fields == null || zone.fields.GetType() != typeof(FieldsInSolid)) { newfield = new FieldsInSolid(); }
//            break;
//        case PhysicalTypes.流体:
//            switch (zone.continuum.flowType)
//            {
//                case FlowTypes.层流:
//                    if (zone.fields == null || zone.fields.GetType() != typeof(FieldsInLaminarFlow)) { newfield = new FieldsInLaminarFlow(); }
//                    break;
//                case FlowTypes.湍流:
//                    if (zone.fields == null || zone.fields.GetType() != typeof(FieldsInTurbulentFlowkw)) { newfield = new FieldsInTurbulentFlowkw(); }
//                    break;
//                default:
//                    return;
//            }
//            break;
//        default:
//            return;

//    }
//    if (newfield != null)
//    {
//        newfield.PickDataFromOld(zone, zone.fields);
//        zone.fields = newfield;
//    }
//    zone.fields.CreateFields(zone);
//    zone.fields.SetParameter();
//}
//public virtual void initialize_ifNeeded(int taskIndex) { }
//public virtual void initialize_force(int taskIndex) { }
//public virtual void updateMatrix(int taskIndex) { }
//public virtual void solve(int taskIndex) { }